/*global WebViewJavascriptBridge: true */
import userAgent from './userAgent.js'

export default {
  init (callback) {
    if (window.WebViewJavascriptBridge) {
      return callback(WebViewJavascriptBridge)
    } else {
      document.addEventListener('WebviewJavascriptBridgeReady', () => {
        callback(WebViewJavascriptBridge)
      }, false)
    }

    if (userAgent.getOS() === 'ios') {
      if (window.WVJBCallbacks) {
        return window.WVJBCallbacks.push(callback)
      }
      window.WVJBCallbacks = [callback]
      let WVJBIframe = document.createElement('iframe')
      WVJBIframe.style.display = 'none'
      WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__'
      document.documentElement.appendChild(WVJBIframe)
      setTimeout(() => {
        document.documentElement.removeChild(WVJBIframe)
      }, 0)
    }
  }
}

